package com.mj.mes.modbus;/**
 * @author cRyann
 * @create 2017-06-27 13:07
 **/

import com.serotonin.modbus4j.exception.ModbusTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author cRyann
 * @create 2017-06-27 13:07
 **/
@Component
public class ModbusService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    ModbusMasterWrapper modbusMasterWrapper;
//    @Autowired
//    @Value("${MODBUS_SERVERIP}")
//    private String hostIp;
//    @Value("${MODBUS_PORT}")
//    private int hostPort;
//    @Value("${MODBUS_SLAVE}")
//    private int slaveId;
    //

    public void CarModbusW(int carNum, int satation) throws ModbusTransportException {
        modbusMasterWrapper.connect();
        short[] sh = modbusMasterWrapper.readHoldingRegisterValues(0, 1);
        for (int i = 0; i < sh.length; i++) {
            if (sh[i] == satation) {
                sh[i] = 0;
            }
        }
        sh[carNum - 1] = (short) satation;
//        modbusMasterWrapper.writeRegisterValues(addr_car, sh);
        logger.debug("Write Success carNum:{}, satation{}", carNum, satation);
        modbusMasterWrapper.disconnect();
    }

    public String CarStatusModbusR(String addr_carstatus) throws ModbusTransportException {
//        ModbusMasterWrapper modbusMasterWrapper = new ModbusMasterWrapper(hostIp, hostPort, slaveId);
        modbusMasterWrapper.connect();
        short[] sh = modbusMasterWrapper.readHoldingRegisterValues(Integer.parseInt(addr_carstatus), 0);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < sh.length; i++) {
            sb.append(String.valueOf(sh[i]));
        }
        logger.debug("Read Success {}", sb.toString());
        modbusMasterWrapper.disconnect();
        return sb.toString();
    }

    public void CarWorkRestW(String data, String split) throws ModbusTransportException {
        short[] sh = new short[12];
        if (data != null && !data.equals("")) {
            String[] strs = data.split(split);
            for (int i = 0; i < strs.length && i < 12; i++) {
                sh[i] = Short.parseShort(strs[i]);
            }
//            ModbusMasterWrapper modbusMasterWrapper = new ModbusMasterWrapper(hostIp, hostPort, slaveId);
            modbusMasterWrapper.connect();
            modbusMasterWrapper.writeRegisterValues(0, sh);
            logger.debug("Write Success {}", data);
            modbusMasterWrapper.disconnect();
        }
    }

    public void IsAlive(short num) throws ModbusTransportException {
//        ModbusMasterWrapper modbusMasterWrapper = new ModbusMasterWrapper(hostIp, hostPort, slaveId);
        if (modbusMasterWrapper.connect()) {
            modbusMasterWrapper.writeRegisterValue(0, num);
            logger.debug("Write Success {},{}", 0, num);
        }
        modbusMasterWrapper.disconnect();
    }
}